package com.flyan.game.motafx.event;

import com.almasb.fxgl.dsl.FXGL;
import com.flyan.game.motafx.core.event.BaseEvent;
import com.flyan.game.motafx.core.event.enums.EventTypeEnum;

/**
 * 设置游戏属性事件
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/2/10
 */
public class SetPropertyEvent extends BaseEvent {

    @Override
    public String type() {
        return EventTypeEnum.SET_PROPERTY.getCode();
    }

    @Override
    public void occur() {
        var name = properties.getStr("name");
        var type = properties.getStr("type");
        if (type != null) {
            /* 给定了值类型，需要先转换 */
            try {
                var typeClass = ClassLoader.getSystemClassLoader().loadClass(type);
                var value = properties.get("value", typeClass);
                FXGL.set(name, value);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        } else {
            /* 未指定值类型，一律视为字符串 */
            FXGL.set(name, properties.getStr("value"));
        }
    }

}
